<template>
  <div class="login-container">
    <el-card class="card-login">
      <div class="login-header">
        <h3>后台管理系统</h3>
        <p>使用您的账号密码进行登陆</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    type="text"
                    name="username"
                    autocomplete="on"
                    prefix-icon="el-icon-user"
                    :clearable="true"
                    placeholder="请输入登陆账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    type="password"
                    name="password"
                    autocomplete="on"
                    prefix-icon="el-icon-lock"
                    :show-password="true"
                    placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="button"
                     class="el-button el-button--primary"
                     :loading="loading"
                     style="width: 100%;"
                     @click="submitForm">立即登陆
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'index',
  data: function () {
    const validUsername = (rule, value, callback) => {
      // 还需要通过正则去处理
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validPassword = (rule, value, callback) => {
      // 还需要通过正则去处理
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      loginForm: {
        username: null,
        password: null
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validUsername}],
        password: [{required: true, trigger: 'blur', validator: validPassword}]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({path: '/'}) // 登陆成功之后重定向到首页
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('参数验证不合法！')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .login-container {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: .8;

    background-attachment: fixed;
    background-image: url('./img/bg.jpg');
    background-repeat: no-repeat;
    background-size: cover;

    display: flex;
    align-items: center;
    justify-content: center;
    align-content: center;
  }

  .card-login {
    width: 360px;
  }

  .login-header {
    margin: 15px 0 30px 0;
    text-align: center;
  }

  .login-header h3 {
    font-weight: 600;
    color: #2b425b;
  }

  .login-header p {
    color: #afb9c3;
  }
</style>
